﻿using System;
using System.Linq;
using System.Drawing;
using System.Runtime.CompilerServices;
using DevComponents.DotNetBar;
using System.Windows.Forms;

namespace PlanningGIS.Framework
{
    /// <summary>
    /// 停靠窗体类
    /// </summary>
    public class DockManagerClass : IDockManager
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DockManagerClass()
        {

        }
        
        /// <summary>
        /// 主窗体
        /// </summary>
        private Form _MainForm = null;

        /// <summary>
        /// 主窗体
        /// </summary>
        public Form MainForm
        {
            set
            {
                _MainForm = value;
            }
        }
        
        /// <summary>
        /// 主面板管理控件
        /// </summary>
        private DotNetBarManager _DockManager = null;

        /// <summary>
        /// 主面板管理控件
        /// </summary>
        public object DockManager
        {
            set
            {
                this._DockManager = value as DotNetBarManager;
            }
        }

        /// <summary>
        /// 添加停靠面板
        /// </summary>
        /// <param name="text">停靠窗口名称</param>
        /// <param name="control">停靠窗口中的控件</param>
        /// <param name="isReplace">是否替换控件</param>
        /// <param name="sides">停靠位置</param>
        /// <returns></returns>
        [Obsolete("已废弃")]
        public bool AddDockPanel(string text, Control control, bool isReplace, esriDockFlags sides)
        {
            return AddDockPanel(text, control, sides, text,  isReplace);
        }

        /// <summary>
        /// 添加停靠面板
        /// </summary>
        /// <param name="name">面板名称，不能为空</param>
        /// <param name="text">停靠窗口名称</param>
        /// <param name="control">停靠窗口中的控件</param>
        /// <param name="isReplace">是否替换控件</param>
        /// <param name="sides">停靠位置</param>
        /// <returns></returns>
        public bool AddDockPanel(string name, Control control, esriDockFlags sides, string text = "", bool isReplace = true)
        {
            Bar bar = GetDockBar(name);
            if (bar != null)
            {
                if (!string.IsNullOrWhiteSpace(text)) bar.Text = text;
                if (isReplace)
                {
                    ((DockContainerItem)bar.Items[name]).Control = control;
                    if (!string.IsNullOrWhiteSpace(text)) ((DockContainerItem)bar.Items[name]).Text = text;
                }
                ActivateDockPanel(name, text);
                return true;
            }
            else if (bar == null)
            {
                DockSite docksite = null;
                switch (sides)
                {
                    case esriDockFlags.esriDockLeft:
                        docksite = _DockManager.LeftDockSite;
                        break;
                    case esriDockFlags.esriDockBottom:
                        docksite = _DockManager.BottomDockSite;
                        break;
                    case esriDockFlags.esriDockRight:
                        docksite = _DockManager.RightDockSite;
                        break;
                    case esriDockFlags.esriDockFloat:
                        docksite = _DockManager.FillDockSite;
                        break;
                    default:
                        docksite = _DockManager.LeftDockSite;
                        break;
                }
                if (docksite.Controls.Count == 0)
                {
                    bar = CreateDockBar(name, text);
                }
                else
                {
                    if (docksite.Controls[0] is Bar && !(((Bar)docksite.Controls[0]).AutoHide))
                    {
                        bar = docksite.Controls[0] as Bar;
                    }
                    else
                    {
                        bar = CreateDockBar(name, text);
                    }
                }
                DockContainerItem dockItem = new DockContainerItem(name);
                dockItem.Text = string.IsNullOrWhiteSpace(text) ? name : text;
                dockItem.Control = control;
                //dockItem.Height = 128;
                control.BackColor = Color.Transparent;
                bar.Items.Add(dockItem);
                bar.Visible = true;
                //bar.Height = 200;

                if (docksite.Height < 300)
                    docksite.Height = 300;
                
                
                bar.SelectedDockContainerItem = dockItem;
                if (!docksite.Controls.Contains(bar)) docksite.GetDocumentUIManager().Dock(bar);
                bar.RecalcLayout();
                return true;
            }
            return true;
        }

        /// <summary>
        /// 获取停靠面板中的控件
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Control GetControlFromDockPanel(string name)
        {
            foreach (Bar bar in _DockManager.Bars)
            {
                foreach (BaseItem itm in bar.Items)
                {
                    if (!itm.Text.Equals(name, StringComparison.CurrentCultureIgnoreCase)) continue;
                    DockContainerItem item = itm as DockContainerItem;
                    if (item == null) continue;
                    return item.Control;
                    //PanelDockContainer panel = item.Control as PanelDockContainer;
                    //if (panel != null && panel.Controls.Count > 0)
                    //{
                    //    return panel.Controls[0];
                    //}
                }
            }
            return null;
        }

        /// <summary>
        /// 判断停靠窗体是否已激活
        /// </summary>
        /// <param name="name"></param>
        public bool DockPanelActivated(string name)
        {
            foreach (Bar bar in _DockManager.Bars)
            {
                if (bar.Items.Contains(name))
                {
                    if (!bar.Visible) return false;
                    DockContainerItem item = bar.Items[name] as DockContainerItem;                    
                    return bar.SelectedDockContainerItem == item;
                }
                else
                {
                    for (int i = 0; i < bar.Items.Count; i++)
                    {
                        if (bar.Items[i].Text.Equals(name))
                        {
                            if (!bar.Visible) return false;
                            DockContainerItem item = bar.Items[i] as DockContainerItem;
                            return bar.SelectedDockContainerItem == item;
                        }
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 激活停靠面板容器
        /// </summary>
        /// <param name="name">面板名称</param>
        /// <param name="text">面板标题</param>
        public void ActivateDockPanel(string name, string text = "")
        {
            foreach (Bar bar in _DockManager.Bars)
            {
                if (bar.Items.Contains(name))
                {
                    DockContainerItem item = bar.Items[name] as DockContainerItem;
                    item.Visible = true;
                    bar.SelectedDockContainerItem = item;                  
                    bar.RecalcLayout();
                    bar.Visible = true;
                    if (bar.Parent != null && bar.Parent.Height < 200)
                        bar.Parent.Height = 200;
                    if (!string.IsNullOrWhiteSpace(text)) bar.Text = text;    
                }
                else
                {
                    for (int i = 0; i < bar.Items.Count; i++)
                    {
                        if (bar.Items[i].Text.Equals(name))
                        {
                            DockContainerItem item = bar.Items[i] as DockContainerItem;
                            item.Visible = true;
                            bar.SelectedDockContainerItem = item;                                                                          
                            bar.Visible = true;
                            //bar.Height = 200;
                            if (bar.Parent != null && bar.Parent.Height < 200)
                                bar.Parent.Height = 200;
                            if (!string.IsNullOrWhiteSpace(text)) bar.Text = text;
                            bar.RecalcLayout();      
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 激活停驻面板，使该面板处于最前
        /// </summary>
        /// <param name="name">面板名称</param>
        public void UnActivateDockPanel(string name)
        {
            foreach (Bar bar in _DockManager.Bars)
            {
                //if (bar.Items.Contains(name))
                //{
                //    //DockContainerItem item = bar.Items[itemText] as DockContainerItem;
                //    //bar.SelectedDockContainerItem = item;
                //    //bar.RecalcLayout();
                //    bar.Visible = false;
                //}
                //else
                //{
                //    for (int i = 0; i < bar.Items.Count; i++)
                //    {
                //        if (bar.Items[i].Text.Equals(name))
                //        {
                //            //DockContainerItem item = bar.Items[i] as DockContainerItem;
                //            //item.Visible = true;
                //            //bar.SelectedDockContainerItem = item;
                //            bar.Visible = false;
                //            //bar.RecalcLayout();
                //        }
                //    }
                //}

                foreach (DockContainerItem item in bar.Items)
                {
                    if (item.Text.Equals(name))
                    {
                        item.Visible = false;
                        bool allClose = true;
                        foreach (DockContainerItem item1 in bar.Items)
                        {
                            if (item1.Visible == true) allClose = false;
                        }
                        if (allClose) bar.Visible = false;
                        return;
                    }                        
                }
            }
        }

        /// <summary>
        /// 移除停靠容器
        /// </summary>
        /// <param name="name"></param>
        public void RemoveDockPanel(string name)
        {
            Bar bar = GetDockBar(name);
            if (bar == null) return;
            bar.Items.Remove(name);
            bar.RecalcLayout();
        }

        /// <summary>
        /// 创建停靠容器
        /// </summary>
        /// <param name="name">面板名称</param>
        /// <param name="text">面板标题</param>
        /// <returns></returns>
        private Bar CreateDockBar(string name, string text = "")
        {
            Bar bar = new Bar(name);
            bar.CanDockTop = false;
            bar.CanUndock = false;
            bar.SaveLayoutChanges = false;
            bar.Name = name;
            bar.Text = string.IsNullOrWhiteSpace(text) ? name : text;
            bar.LayoutType = eLayoutType.DockContainer;
            bar.Stretch = true;
            bar.AutoHideAnimationTime = 100;
            bar.GrabHandleStyle = eGrabHandleStyle.Caption;
            bar.Width = 350;
            bar.CloseSingleTab = true;
            bar.CanHide = true;
            _DockManager.Bars.Add(bar);
            return bar;
        }

        /// <summary>
        /// 获取停靠容器
        /// </summary>
        /// <param name="name">面板名称</param>
        /// <returns></returns>
        private Bar GetDockBar(string name)
        {
            foreach (Bar bar in _DockManager.Bars)
            {
                if (bar.Items.Contains(name))
                {
                    return bar;
                }
            }
            return null;
        }

        /// <summary>
        /// 添加主容器面板页签
        /// </summary>
        /// <param name="name">页签名称</param>
        /// <param name="control">面板中的控件</param>
        /// <param name="text">页签标题</param>
        /// <returns></returns>
        public bool AddTabItem(string name, Control control, string text = "")
        {
            if (control == null) return false;
            DevComponents.DotNetBar.TabControl tabControl = GetTabControl();
            if (tabControl == null) return false;
            foreach (TabItem ti in tabControl.Tabs)
            {
                if (ti.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)) return true;
            }
            AddTabItem(name, control, tabControl, text);
            return true;
        }

        /// <summary>
        /// 在主窗口的Tab集合中添加页签
        /// </summary>
        /// <param name="name"></param>
        /// <param name="control"></param>
        /// <param name="tabControl"></param>
        /// <param name="text"></param>
        private void AddTabItem(string name, Control control, DevComponents.DotNetBar.TabControl tabControl, string text = "")
        {
            TabItem ti = new TabItem();
            TabControlPanel tabControlPanel = new TabControlPanel();
            tabControl.Tabs.Add(ti);
            tabControl.Controls.Add(tabControlPanel);
            ti.AttachedControl = tabControlPanel;
            ti.Name = control.Name;
            ti.Text = string.IsNullOrWhiteSpace(text) ? name : text;
            control.Dock = DockStyle.Fill;
            tabControlPanel.Controls.Add(control);
            tabControlPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            tabControlPanel.Location = new System.Drawing.Point(0, 25);
            tabControlPanel.Name = control.Name + "TabControlPanel";
            tabControlPanel.Padding = new System.Windows.Forms.Padding(1);
            tabControlPanel.Size = new System.Drawing.Size(496, 264);
            tabControlPanel.Style.BackColor1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(242)))), ((int)(((byte)(232)))));
            tabControlPanel.Style.BackColor2.Color = System.Drawing.Color.White;
            tabControlPanel.Style.Border = DevComponents.DotNetBar.eBorderType.SingleLine;
            tabControlPanel.Style.BorderColor.Color = System.Drawing.Color.FromArgb(((int)(((byte)(172)))), ((int)(((byte)(168)))), ((int)(((byte)(153)))));
            tabControlPanel.Style.BorderSide = ((DevComponents.DotNetBar.eBorderSide)(((DevComponents.DotNetBar.eBorderSide.Left | DevComponents.DotNetBar.eBorderSide.Right)
                        | DevComponents.DotNetBar.eBorderSide.Bottom)));
            tabControlPanel.Style.GradientAngle = 90;
            tabControlPanel.TabIndex = 3;
            tabControlPanel.TabItem = ti;
            tabControl.Refresh();
        }
        
        /// <summary>
        /// 激活主窗口中的页签
        /// </summary>
        /// <param name="name">页签显示名称</param>
        public void ActivateTabItem(string name)
        {
            DevComponents.DotNetBar.TabControl tabControl = GetTabControl();
            if (tabControl == null || tabControl.Tabs == null) return;
            foreach (TabItem ti in tabControl.Tabs)
            {
                if (ti.Text.Equals(name, StringComparison.CurrentCultureIgnoreCase))
                    tabControl.SelectedTab = ti;
            }
            
        }

        /// <summary>
        /// 获取主窗口中的Tab控件
        /// </summary>
        /// <returns></returns>
        private DevComponents.DotNetBar.TabControl GetTabControl()
        {
            if (_DockManager == null || _DockManager.Bars == null) return null;
            foreach (Bar bar in _DockManager.Bars)
            {
                foreach (BaseItem bi in bar.Items)
                {
                    if (bi is DockContainerItem)
                    {
                        DockContainerItem dockContainerItem = (DockContainerItem)bi;
                        if (dockContainerItem.Control is PanelDockContainer)
                        {
                            PanelDockContainer panelDockContainer = (PanelDockContainer)dockContainerItem.Control;
                            if (panelDockContainer.Controls == null) return null;
                            foreach (Control ctl in panelDockContainer.Controls)
                            {
                                if (ctl is DevComponents.DotNetBar.TabControl)
                                {
                                    DevComponents.DotNetBar.TabControl tabControl = (DevComponents.DotNetBar.TabControl)ctl;
                                    return tabControl;
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }


    }

    
}
